#ifndef CHARACTERFEATURE_H
#define CHARACTERFEATURE_H

#include "dtwenty_global.h"
#include "dtwentyitem.h"
#include "dtwentyitemcontainer.h"
#include "categories.h"


#include <QTimer>
#include <QMetaEnum>


typedef CategoryBase CharacterFeature;


class DTWENTYSHARED_EXPORT CharacterFeatures : public DTwentyItemContainer
{
    Q_OBJECT

public:


    Q_INVOKABLE CharacterFeatures(QObject* parent = 0) :
        DTwentyItemContainer(parent)
    {
        registerChildType<CharacterFeature>();
        qRegisterMetaType<BaseAttackBonus*>();
        qRegisterMetaType<HitDie*>();
        qRegisterMetaType<SkillPoints*>();
        qRegisterMetaType<SaveBonus*>();

        QTimer::singleShot(0, this, SLOT(onInitChildren()));
    }

    CharacterFeatures(const CharacterFeatures& other) :
        DTwentyItemContainer(other)
    {
    }

    virtual int type() const
    {
        return qMetaTypeId<CharacterFeatures*>();
    }

protected slots:

    virtual void onInitChildren()
    {
        CharacterFeature* feature = findChild<HitDie*>(QString(), Qt::FindDirectChildrenOnly);
        if (!feature)
        {
            (void) new HitDie(this);
        }

        feature = findChild<SkillPoints*>(QString(), Qt::FindDirectChildrenOnly);
        if (!feature)
        {
            (void) new SkillPoints(this);
        }

        feature = findChild<BaseAttackBonus*>(QString(), Qt::FindDirectChildrenOnly);
        if (!feature)
        {
            (void) new BaseAttackBonus(this);
        }

        QList<SaveBonus*> saves = findChildren<SaveBonus*>(QString(), Qt::FindDirectChildrenOnly);
        if (saves.count() == 3)
        {
            return;
        }

        foreach (CharacterFeature* save, saves)
        {
            save->setParent(0);
            save->deleteLater();
        }

        (void) new SaveBonus(tr("Fort"), this);
        (void) new SaveBonus(tr("Ref"), this);
        (void) new SaveBonus(tr("Will"), this);


#if 0
        const QMetaObject* mo = metaObject();
        QMetaEnum me = mo->enumerator(mo->enumeratorOffset());


        // generate hit die, skill points, b.a, and saves if not exist
        QList<CharacterFeature*> features = findChildren<CharacterFeature*>(QString(), Qt::FindDirectChildrenOnly);
        for (int id = 0; id < Feature_CUSTOM; id++)
        {
            bool exists = false;
            foreach (const CharacterFeature* feature, features)
            {
                if (id == feature->id())
                {
                    exists = true;
                    break;
                }
            }

            if (exists)
            {
                continue;
            }

            CharacterFeature* feature = new CharacterFeature(this);
            feature->setName(me.valueToKey(id));
            feature->setId(id);

            const QMetaObject* fmo = feature->metaObject();
            int enumCount = fmo->enumeratorCount();
            int enumPos = id+fmo->enumeratorOffset();
            if (enumPos >= enumCount)
            {
                enumPos = enumCount-1;
            }

            QMetaEnum fme = fmo->enumerator(enumPos);
            feature->setValue(fme.value(0));
        }
#endif
    }
};

Q_DECLARE_METATYPE(CharacterFeatures)


#endif // CHARACTERFEATURE_H

